﻿using System;
using System.Web;
using System.Web.Mvc;
using Toast.Caching;
using Toast.HttpModules;

namespace Toast.Core
{
    public class ToastCore
    {
        internal const string HANDLER_FILENAME = "Include.axd";
        internal const string COMMON_FILENAME = "__common";
        internal const string REQUEST_FILENAME = "__request";
        internal static string HANDLER_URL_SEGMENT = "/" + HANDLER_FILENAME + "?d={0}&p={1}";
        internal static bool Initialized;

        static readonly DefaultCompressionDictionary _defaultCompression = new DefaultCompressionDictionary();
        static readonly ContentCollection CommonContentInstance = new ContentCollection();
        static bool _debug = HttpContext.Current != null ? HttpContext.Current.IsDebuggingEnabled : false;
        static InjectAt _defaultInjectAt = InjectAt.HeadBottom;
        
        public static ToastHttpModule ToastModule = new ToastHttpModule();

        public static bool SeperateFiles
        {
            get { return _debug; }
            set
            {
                _debug = value;
                HttpRuntimeCacheService.Clear();
            }
        }

        public static InjectAt DefaultInjectAt
        {
            get { return _defaultInjectAt; }
            set { _defaultInjectAt = value != InjectAt.Default ? value : InjectAt.HeadBottom; }
        }

        internal static string ApplicationRuntimeId { get; set; }

        public static DefaultCompressionDictionary Compression()
        {
            return _defaultCompression;
        }

        public static DefaultCompressionDictionary Compression(Action<DefaultCompressionDictionary> action)
        {
            action(_defaultCompression);
            return _defaultCompression;
        }

        public static ContentCollection CommonContent()
        {
            return CommonContentInstance;
        }

        public static ContentCollection CommonContent(Action<ContentCollection> action)
        {
            action(CommonContentInstance);

            return CommonContentInstance;
        }

        public static void WrapViewEngines()
        {
            for (var idx = 0; idx < ViewEngines.Engines.Count; idx++)
            {
                var ve = ViewEngines.Engines[idx];
                if (!(ve is WebFormViewEngine) || (ve is ToastViewEngineWrapper))
                    continue;
                ViewEngines.Engines.Remove(ve);
                ViewEngines.Engines.Insert(idx, ToastViewEngineWrapper.Wrap(ve as WebFormViewEngine));
            }
        }

        public static void Initialize(HttpApplication httpApplication)
        {
            ApplicationRuntimeId = ShortGuid.NewGuid();

            ToastModule.Init(httpApplication);
            WrapViewEngines();

            // TOOD: Maybe do something like this to inject an authorized handler location into the web.config
            //var conf = WebConfigurationManager.OpenWebConfiguration("~");
            //var swsg = (SystemWebSectionGroup)conf.GetSectionGroup("configuration");

            //var rule = new AuthorizationRule(AuthorizationRuleAction.Allow);
            //rule.Users.Add("*");
            //swsg.Authorization.Rules.Add(rule);

            //conf.Save(ConfigurationSaveMode.Modified);

            Initialized = true;
        }
    }
}